package svc

import (
	"fmt"

	"mall/service/mqueue/job/internal/config"

	"github.com/hibiken/asynq"
	"github.com/zeromicro/go-zero/core/service"
)

func newAsynqServer(c config.Config) *asynq.Server {
	if c.Mode != service.ProMode {
		return asynq.NewServer(
			asynq.RedisClientOpt{
				Addr:     c.BizRedis.Host,
				Password: c.BizRedis.Pass,
			},
			asynq.Config{
				IsFailure: func(err error) bool {
					fmt.Printf("asynq server exec task IsFailure ======== >>>>>>>>>>>  err: %+v \n", err)
					// ScanAttachmentLogProcessor err: rpc error: code = Unimplemented desc = unknown method ScanAttachmentLog for service attachment.Attachment: skip retry for the task
					// ScanAttachmentLogProcessor err: rpc error: code = Unavailable desc = last connection error: connection error: desc = "transport: Error while dialing: dial tcp 172.18.0.2:9002: connect: connection refused"; last resolver error: produced zero addresses: skip retry for the task
					// asynq: pid=17923 2025/02/12 12:39:00.783099 WARN: Retry exhausted for task id=4cb80a26-f7f7-4859-898e-dae1ca6c0c1f
					return true
				},
				// Specify how many concurrent workers to use
				Concurrency: 20,
			},
		)
	}

	return asynq.NewServer(
		asynq.RedisClusterClientOpt{
			Addrs:    []string{c.BizRedis.Host},
			Password: c.BizRedis.Pass,
		},
		asynq.Config{
			IsFailure: func(err error) bool {
				fmt.Printf("asynq server exec task IsFailure ======== >>>>>>>>>>>  err : %+v \n", err)
				return true
			},
			Concurrency: 20,
		},
	)
}
